import com.southminority.ethnic.column.ExcelColumn;
import com.southminority.ethnic.pojo.User;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.ReflectionUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SpringBootTest
public class excelT {
    /**
     * 导出Excel
     * @param dataList 数据列表
     * @param <T> 实体类类型
     * @return Excel文件字节数组
     */
    public static <T> byte[] exportExcel(List<T> dataList) throws IOException, IllegalAccessException {
        if (dataList == null || dataList.isEmpty()) {
            throw new IllegalArgumentException("数据列表不能为空");
        }

        // 获取实体类类型
        Class<?> clazz = dataList.get(0).getClass();

        // 创建工作簿和工作表
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("数据");

        // 获取所有字段（包括父类字段）
        List<Field> fields = getAllFields(clazz);

        // 创建表头行
        Row headerRow = sheet.createRow(0);

        // 填充表头
        for (int i = 0; i < fields.size(); i++) {
            Field field = fields.get(i);
            field.setAccessible(true);

            // 确定列名
            String columnName;
            ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class);
            if (excelColumn != null && !excelColumn.name().isEmpty()) {
                columnName = excelColumn.name();
            } else {
                columnName = field.getName();
            }

            // 创建单元格并设置值
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(columnName);
        }

        // 填充数据行
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < dataList.size(); i++) {
            T data = dataList.get(i);
            Row dataRow = sheet.createRow(i + 1); // 从第二行开始

            for (int j = 0; j < fields.size(); j++) {
                Field field = fields.get(j);
                field.setAccessible(true);

                Object value = field.get(data);
                Cell cell = dataRow.createCell(j);

                // 根据值的类型设置单元格内容
                if (value != null) {
                    if (value instanceof String) {
                        cell.setCellValue((String) value);
                    } else if (value instanceof Number) {
                        cell.setCellValue(((Number) value).doubleValue());
                    } else if (value instanceof Date) {
                        cell.setCellValue(sdf.format((Date) value));
                    } else if (value instanceof Boolean) {
                        cell.setCellValue((Boolean) value);
                    } else {
                        cell.setCellValue(value.toString());
                    }
                }
            }
        }

        // 自动调整列宽
        for (int i = 0; i < fields.size(); i++) {
            sheet.autoSizeColumn(i);
        }

        // 将工作簿写入字节数组
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();

        return outputStream.toByteArray();
    }

    /**
     * 获取类及其父类的所有字段
     */
    private static List<Field> getAllFields(Class<?> clazz) {
        List<Field> fieldList = new ArrayList<>();
        while (clazz != null && clazz != Object.class) {
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                fieldList.add(field);
            }
            clazz = clazz.getSuperclass();
        }
        return fieldList;
    }
    @Test
    public void test() {
        List<User> dataList = new ArrayList<>();
        User user = new User();
        user.setUsername("张三");
        user.setPassword("123456");
        user.setEmail("zhangsan@example.com");
        user.setPhone("13800000000");
        user.setAvatar("https://example.com/avatar.jpg");
        user.setToken("1234567890");
        user.setUserType(0);
        dataList.add(user);


    }
}




